package tenantchannellogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type KbzpayListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewKbzpayListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KbzpayListLogic {
	return &KbzpayListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *KbzpayListLogic) KbzpayList(in *rpc.KbzpayListRequest) (*rpc.KbzpayListResponse, error) {
	var (
		CommonResp rpc.CommonResp
		kbzpayList []schema.TenantChannelKbzpay
		err        error
	)

	kbzpayModel := model.NewTenantChannelKbzpayModel(l.ctx, l.svcCtx.Db)
	if in.Code != "" {
		kbzpayList, err = kbzpayModel.GetKbzpayByCode(in.Code)
	} else {
		kbzpayList, err = kbzpayModel.GetAllKbzpay()
	}

	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.KbzpayListResponse{
			CommonResp: &CommonResp,
		}, err
	}

	var records []*rpc.Kbzpay
	for _, kbzpay := range kbzpayList {
		records = append(records, &rpc.Kbzpay{
			Id:           kbzpay.ID,
			CreatedAt:    kbzpay.CreatedAt,
			UpdatedAt:    kbzpay.UpdatedAt,
			Code:         kbzpay.Code,
			Phone:        kbzpay.Phone,
			CardHolder:   kbzpay.CardHolder,
			MinAmount:    kbzpay.MinAmount,
			MaxAmount:    kbzpay.MaxAmount,
			DailyTimes:   kbzpay.DailyTimes,
			DailyAmount:  kbzpay.DailyAmount,
			RemainTimes:  kbzpay.RemainTimes,
			RemainAmount: kbzpay.RemainAmount,
			VisionLevel:  kbzpay.VisionLevel,
			IsDisplay:    kbzpay.IsDisplay,
		})
	}

	CommonResp.Message = "Success"
	return &rpc.KbzpayListResponse{
		Records:    records,
		CommonResp: &CommonResp,
	}, nil
}
